bl_info = {
    "name": "CustomGroup",
    "author": "会飞的键盘侠",
    "version": (0, 0, 1),
    "blender": (3, 0, 0),
    "location": "3DView->UI",
    "category": "自定义组"
}

# 1. 定义组
group = {
    "mygroup": [],
    "mygroup2": [],
}
# 2. 物体组
#    将所选物体添加到物体组
# 3. 集合组
#    将所选的集合添加到集合组
# 4. UIList项
#    如何更新 timer
# 5. UIList显示面板

# *. 后续可以如何优化?
#    将 所有数据都用 UIList项存储, 每个组都是一个Item, 组内的元素存入一个CollectionPropety

import bpy
from .uilist import uilist_reg, uilist_unreg

class ObjGroupOps(bpy.types.Operator):
    """
    添加所选物体到物体组
    """
    bl_idname = "cg.objgroup"
    bl_label = "物体组"
    bl_description = bl_label
    bl_options = {"REGISTER", "UNDO"}
    group_name: bpy.props.StringProperty(default="default")
    last_name = ""

    def invoke(self, context: bpy.types.Context, event: bpy.types.Event):
        ObjGroupOps.last_name = ""
        return {"FINISHED"}

    def execute(self, context: bpy.types.Context):
        if "OBJECT" not in bpy.context.scene.cg_item:
            item = bpy.context.scene.cg_item.add()
            item.name = "OBJECT"
        
        from .data import GROUP_OBJ
        if ObjGroupOps.last_name in GROUP_OBJ:
            # 将上次的名称更改为当前名称
            group = GROUP_OBJ.pop(ObjGroupOps.last_name)
            GROUP_OBJ[self.group_name] = group
        else:
            # 全新添加
            GROUP_OBJ[self.group_name] = bpy.context.selected_objects[:]
            ObjGroupOps.last_name = self.group_name
        return {"FINISHED"}

    def draw(self, context: bpy.types.Context):
        self.layout.prop(self, "group_name")


class GetSelCol(bpy.types.Operator):
    bl_idname = 'cg.get_sel_cols'
    bl_label = 'Selected Collections'
    cols = []

    def execute(self, context):
        selection = context.selected_ids
        selected_collections = [sel for sel in selection if sel.rna_type.name == 'Collection']
        GetSelCol.cols.clear()
        GetSelCol.cols.extend(selected_collections)
        return {'FINISHED'}


class ColGroupOps(bpy.types.Operator):
    """
    添加所选集合到集合组
    """
    bl_idname = "cg.colgroup"
    bl_label = "集合组"
    bl_description = bl_label
    bl_options = {"REGISTER", "UNDO"}
    group_name: bpy.props.StringProperty(default="default")
    last_name = ""

    def invoke(self, context: bpy.types.Context, event: bpy.types.Event):
        ColGroupOps.last_name = ""
        return {"FINISHED"}

    def execute(self, context: bpy.types.Context):
        if "COLLECTION" not in bpy.context.scene.cg_item:
            item = bpy.context.scene.cg_item.add()
            item.name = "COLLECTION"
            
        from .data import GROUP_COL
        if ColGroupOps.last_name in GROUP_COL:
            # 将上次的名称更改为当前名称
            group = GROUP_COL.pop(ColGroupOps.last_name)
            GROUP_COL[self.group_name] = group
        else:
            # 全新添加
            area = [area for area in bpy.context.screen.areas if area.type == "OUTLINER"][0]
            with bpy.context.temp_override(area=area):
                bpy.ops.cg.get_sel_cols()
            GROUP_COL[self.group_name] = GetSelCol.cols[:]
            ColGroupOps.last_name = self.group_name
        return {"FINISHED"}

    def draw(self, context: bpy.types.Context):
        self.layout.prop(self, "group_name")


class CustomGroupPanel(bpy.types.Panel):
    bl_label = "CG_PT_UI"
    bl_description = "自定义组"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "自定义组"

    def draw(self, context):
        layout = self.layout
        layout.label(text="测试")
        layout.operator(ObjGroupOps.bl_idname)
        layout.operator(ColGroupOps.bl_idname)
        sce = bpy.context.scene
        
        layout.template_list("CG_UI_UIList", "", sce, "cg_item", sce, "cg_item_index")


reg, unreg = bpy.utils.register_classes_factory([ObjGroupOps, ColGroupOps, GetSelCol, CustomGroupPanel])


def register():
    reg()
    uilist_reg()


def unregister():
    unreg()
    uilist_unreg()